package edu.westga.sketchit2009cs.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.*;

import edu.westga.sketchit2009cs.controller.Sketchit2009csApplication;
import edu.westga.sketchit2009cs.network.*;
import edu.westga.sketchit2009cs.stringBufferServer.DrawingGrabber;

/**
 * GameInterface defines the framework of the GUI for the game. Its content pane
 * will be occupied only by a game interface panel, where the user can play the
 * game.
 * 
 * @author Steven Campbell, RJ Hill, Daniel Reeves, Fall 2009
 * 
 */
public class GameInterface extends JPanel implements KeyListener {
	private JPanel drawingPanel;
	private JPanel scorePanel;
	private JPanel scoreChatPanel;
	private JPanel gamePanel;
	JScrollPane chatScroll;
	public static PaintPanel drawingPane;
	public JTextField chatTextField;
	private JLabel categoryTextLabel;
	private JButton requestDrawingButton;
	private static final long serialVersionUID = 1L;
	public final String CATEGORY_STRING_VALUE = "Category:";
	public final String NAME_STRING_VALUE = "Name";
	public final String SCORE_STRING_VALUE = "Score";
	public final String NULL_STRING_VALUE = "";
	public final String NEW_LINE_STRING_VALUE = "\n";
	private DrawingGrabber theGrabber;
	public String thePlayersName;
	public String theIpAddress;
	private boolean grabberRunning = false;

	/**
	 * Creates an instance of the GUI with the content pane containing the game
	 * interface panel. The GUI will not be visible when created.
	 */
	public GameInterface(String ipAddress) {
		super();
		thePlayersName = Sketchit2009csMainFrame.playerName;
		theIpAddress = ipAddress;
		this.build();
		theGrabber = new DrawingGrabber();
	}

	// Creates the Game Interface panel on the current GUI.
	public void build() {
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.setMaximumSize(new Dimension(600, 600));
		this.setMinimumSize(new Dimension(600, 600));
		this.gamePanel = new JPanel();
		this.gamePanel
				.setLayout(new BoxLayout(this.gamePanel, BoxLayout.X_AXIS));

		this.gamePanel.add(Box.createRigidArea(new Dimension(20, 20)));
		this.buildScoreAndChatArea();
		this.gamePanel.add(Box.createRigidArea(new Dimension(20, 20)));
		this.buildDrawingArea();
		this.gamePanel.add(Box.createRigidArea(new Dimension(20, 20)));

		this.setBackground(Color.LIGHT_GRAY);
		this.gamePanel.setBackground(Color.LIGHT_GRAY);
		this.add(this.gamePanel);

	}

	// Builds the Components for the drawingPanel.
	private void buildDrawingArea() {
		this.buildDrawingPane();
		this.buildCategoryTextLabel();
	}

	// Creates the drawing pane
	private void buildDrawingPane() {
		this.drawingPanel = new JPanel();
		this.drawingPanel.setBackground(Color.LIGHT_GRAY);

		GameInterface.drawingPane = new PaintPanel();
		GameInterface.drawingPane.setMaximumSize(new Dimension(400, 350));
		this.drawingPanel.setLayout(new BoxLayout(this.drawingPanel,
				BoxLayout.Y_AXIS));
		this.requestDrawingButton = new JButton("Grab Drawing");
		requestDrawingButton.addActionListener(new grabDrawingActionListener());
		this.drawingPanel.add(GameInterface.drawingPane);
		this.drawingPanel.add(this.requestDrawingButton);
		
	}

	// Creates the category text label
	private void buildCategoryTextLabel() {
		JPanel categoryPanel = new JPanel();
		categoryPanel.setBackground(Color.LIGHT_GRAY);
		this.categoryTextLabel = new JLabel();
		this.categoryTextLabel.setText(CATEGORY_STRING_VALUE);
		categoryPanel.setLayout(new BoxLayout(categoryPanel, BoxLayout.X_AXIS));
		categoryPanel.add(this.categoryTextLabel);
		this.drawingPanel.add(categoryPanel);
		this.gamePanel.add(drawingPanel);
	}

	// builds the components for the score and chat area
	private void buildScoreAndChatArea() {
		this.buildScoreBoardTable();
	}

	// Creates the score board table
	private void buildScoreBoardTable() {
		/*
		 * String[][] string1 = new String[][] { { NAME_STRING_VALUE,
		 * SCORE_STRING_VALUE } }; String[] string2 = new String[] {
		 * NAME_STRING_VALUE, SCORE_STRING_VALUE };
		 * 
		 * TableModel scoreBoardTableModel = new DefaultTableModel(string1,
		 * string2); this.scoreBoardTable = new JTable();
		 * this.scoreBoardTable.setModel(scoreBoardTableModel);
		 */
		this.scoreChatPanel = new JPanel();
		// scoreBoardTable.setMaximumSize(new Dimension(300, 20));
		this.scoreChatPanel.setLayout(new BoxLayout(this.scoreChatPanel,
				BoxLayout.Y_AXIS));
		this.scoreChatPanel.setBackground(Color.LIGHT_GRAY);
		this.scorePanel = new JPanel();
		this.scorePanel.setLayout(new BoxLayout(this.scorePanel,
				BoxLayout.Y_AXIS));
		// this.scorePanel.add(scoreBoardTable);
		this.scorePanel.setBackground(Color.LIGHT_GRAY);
		this.scoreChatPanel.add(scorePanel);
		ChatClient theChatClient = new ChatClient(thePlayersName, theIpAddress,
				Sketchit2009csMainFrame.hostAndJoinPortNumber);
		Sketchit2009csApplication.threadManager.execute(theChatClient);
		this.scoreChatPanel.add(theChatClient);
		this.gamePanel.add(this.scoreChatPanel);
	}

	// This is the Drawing key press event so that users will be able to recieve
	// a drawing.
	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_ENTER) {

			
		}
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}
	
	// Action listener for the drawingGrabber button 
	private class grabDrawingActionListener implements ActionListener {

		/*
		 * Grabs drawings from the server one hundred times. hopefully will be replaced.
		 */
		@Override
		public void actionPerformed(ActionEvent e) {
			if(grabberRunning == false){
			Sketchit2009csApplication.threadManager.execute(theGrabber);
			grabberRunning = true;
			}
		}
	}
}